package ru.yermak.bookkeeping.operations;

import org.springframework.context.annotation.Scope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.yermak.bookkeeping.common.Command;
import ru.yermak.bookkeeping.common.BookkeeperState;
import ru.yermak.bookkeeping.common.UserDao;
import ru.yermak.bookkeeping.ui.ModalForm;
import ru.yermak.bookkeeping.ui.Selector;
import ru.yermak.bookkeeping.model.Operation;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Date;

/**
 * User: harrier
 * Date: Apr 13, 2009
 */
@Scope("action")
@org.springframework.stereotype.Component
public class ProfitCreateCommand implements Command {

    private final Log log = LogFactory.getLog(this.getClass());
    @Autowired
    private ModalForm modalForm;
    @Autowired
    private BookkeeperState bookkeeperState;
    @Autowired
    private UserDao userDao;
    @Autowired
    @OperationsQulifier("profitSelector")
    private Selector selector;

    @Autowired
    private OperationDao operationDao;

    @Autowired
    @OperationsQulifier("profitCreateForm")
    private JComponent accountCreateForm;

    @Autowired
    @OperationsQulifier("profitCreateAmount")
    private JTextField profitCreateAmountField; 


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void execute(ActionEvent e) {
        boolean result = modalForm.show(accountCreateForm);
        if (result) {
            Operation operation = new Operation();
            operation.setAccount(null);
            operation.setCurrency(null);
            operation.setStamp(new Date());
            operation.setUser(userDao.getUserById(bookkeeperState.getUserId()));
//            Integer operationId = operationDao.insertOperation(operation);
//            selector.rowInserted(operationId);
        }

    }
}
